package com.huluobo.roomdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private Button insert, delete, update, query;
    private EditText username, password;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        insert = findViewById(R.id.insert_btn);
        delete = findViewById(R.id.delete_btn);
        update = findViewById(R.id.update_btn);
        query = findViewById(R.id.query_btn);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        result = findViewById(R.id.result_tv);


        insert.setOnClickListener(v -> {
            String usernameString = username.getText().toString();
            String passwordString = password.getText().toString();
            UserBean userBean = new UserBean(
                    usernameString, passwordString
            );
            AppDBUtils.getInstance().getUserDao().insertUserInfo(userBean);
        });

        update.setOnClickListener(v -> {
            String usernameString = username.getText().toString();
            UserBean userBean = AppDBUtils.getInstance().getUserDao().queryUserInfoByName(usernameString);
            String passwordString = password.getText().toString();
            userBean.setPassword(passwordString);
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        delete.setOnClickListener(v -> {
            String usernameString = username.getText().toString();
            UserBean userBean = AppDBUtils.getInstance().getUserDao().queryUserInfoByName(usernameString);
            AppDBUtils.getInstance().getUserDao().deleteUserInfo(userBean);
        });

        query.setOnClickListener(v -> {
            result.setText(AppDBUtils.getInstance().getUserDao().queryUserInfo().toString());
        });
    }
}